<template>
    <!-- 活动报名 -->
    <view class="signup">
        <st-uni-dynamic-form :form-items="formItems" button-text="下一步" @submit-form="submitForm"></st-uni-dynamic-form>
        <text v-if="!this.$store.state.userInfo.gender" class="info-text">备注：性别更改将会同步到个人资料</text>
    </view>
</template>

<script>
    import StUniDynamicForm from '../../components/StUniDynamicForm'

    export default {
        components: {StUniDynamicForm},
        data() {
            return {
                formItems: [
                    {
                        required: true,
                        eType: 'TEXT',
                        name: 'name',
                        label: '用户名'
                    },
                    {
                        required: true,
                        eType: 'TEXT',
                        name: 'mobile',
                        label: '手机号',
                        ruleType: 'mobile'
                    },
                    {
                        required: true,
                        eType: 'TEXT',
                        name: 'idCardNo',
                        label: '身份证',
                        placeholder: '身份证号（仅买保险用）'
                    },
                    {
                        required: true,
                        eType: 'CHECKBOX_DICT',
                        name: 'genderDictId',
                        label: '性别',
                        type: 'gender'
                    },
                    {
                        eType: 'CHECKBOX',
                        name: 'saveToInfo',
                        label: '',
                        multiple: true,
                        value: ['agreement'],
                        data: [{
                            text: '将该信息同步到我的资料中',
                            value: 'agreement'
                        }]
                    }
                ],
                activityId: '',
                wwdUser: {
                    userId: null,
                    name: '',
                    genderDictId: null,
                    mobile: '',
                    idCardNo: ''
                }
            }
        },
        onLoad(options) {
            this.activityId = options.activityId
            this.getDetail(options.isRequireIdCard)
        },
        computed: {},
        methods: {
            submitForm(data) {
                let formData = {}
                Object.assign(formData, data)

                formData.activityId = this.activityId
                formData.userId = this.wwdUser.userId
                formData.saveToInfo = data.saveToInfo.length > 0 ? true : false

                // 将数据保存到localStaorage中，点击下一步时，会用
                uni.setStorageSync('participate', formData)
                uni.navigateTo({
                    url: '/pages/activity/pay?activityId=' + this.activityId
                })
            },
            // 加载用户信息填充表单
            getDetail(isRequireIdCard) {
                this.$http.get('/user-info/userId/' + this.$store.state.userInfo.id)
                    .then(res =>{
                      this.wwdUser = res.data
                      let form = []
                      Object.assign(form, this.formItems)

                      form.forEach(e => {
                        if (e.name === 'saveToInfo') {
                          return
                        }
                        e.value = res.data[e.name]
                        if (e.name === 'idCardNo') {
                          e.required = isRequireIdCard === 'true' ? true : false
                        }
                      })
                      this.formItems = form
                    })

            }
        }
        , watch: {}
    }
</script>

<style>
    .signup .info-text {
        color: red;
    }
</style>
